﻿
var async = require( 'async' );
var path = require( 'path' );
var rootPath = __dirname;
var Util = require( path.join( rootPath, '../Utils/utility' ) );
var nodeContext = require( path.join( rootPath, "../Database/WorkFlowNodeContext" ) );
var taskContext = require( path.join( rootPath, "../Database/TaskContext" ) );
var userImpl = require( path.join( rootPath, "../../Business/UserImpl") );
var baseFlow = require( path.join( rootPath, "../BaseFlow" ) );

var flowName = "Vacation";
var businessType = Util.BusinessType.Vacation;

/*
 * 启动流程
 */
exports.Start = function ( parameters, successCall ) {

    baseFlow.Operate("Start",flowName,"",businessType, parameters, function( err, result ){

        if( !err ){
            console.log("Start workflow of " + flowName + " OK!");
            if( successCall && typeof successCall == "function" ) successCall( err ,result );    
        }

    });

};

exports.Approve = function ( parameters, successCall ){

    parameters.operate = "Approve";

    var nodeName = "";

    async.series(
    {
        findLastTask: function( callback ){
            taskContext.FindNewestTaskByApplicationID( parameters.ApplicationID , function( err, result){
                nodeName = result.Node;
                callback( err, result );
            });
        },

        approve: function( callback ){
            
            baseFlow.Operate(nodeName, flowName, parameters.operate, businessType, parameters, function( err, result ){
                
                callback( err, result);

            });
        }
    },
    function( err, result){

        if( !err ){
            console.log("Approve workflow of " + flowName + " OK!");   

            if( successCall && typeof successCall == "function" ) successCall( err ,result ); 
        }
    });
    
};

exports.Reject = function ( parameters, successCall ){

    parameters.operate = "Reject";

    var nodeName = "";

    async.series(
    {
        findLastTask: function( callback ){
            taskContext.FindNewestTaskByApplicationID( parameters.ApplicationID , function( err, result){
                nodeName = result.Node;
                callback( err, result );
            });
        },

        reject: function( callback ){
            
            baseFlow.Operate(nodeName, flowName, parameters.operate, businessType, parameters, function( err, result ){
            
                callback( err, result);
            });
        }
    },
    function( err, result){

        if( !err ){

            console.log("Reject workflow of " + flowName + " OK!");
            
            if( successCall && typeof successCall == "function" ) successCall( err ,result );     
        }
    });
    
};

/*
 * 查找直接主管
 */
exports.FindManager = function( parameters , callback ){
   
    //return reportTo in jobInfo
   var creator = parameters.CreatorID * 1;
   
   userImpl.GetMyReportTo( creator,function( result ){
       callback( null , result);
    });
};

/*
 * 查找部门主管
 */
exports.FindDepartManager = function( parameters, callback ){

    //查找直接主管的直接主管作为部门主管

    var operatorID = parameters.OperatorID * 1;

    userImpl.GetMyReportTo( operatorID,function( result ){
       callback( null , result);
    });
    
}

/*
 * 查找HRD
 */
exports.FindHRD = function( parameters, callback ){
    
    callback( null ,7);

};

/*
 * 主管操作
 */
exports.ManagerOperate = function( parameters, callback){
    
    callback (null, parameters.operate);

};

/*
 * 部门主管操作
 */
exports.DepartManagerOperate = function(parameters, callback){

    callback(null, parameters.operate);

};

/*
 * HRD操作
 */
exports.HRDOperate = function(parameters, callback){

    callback(null, parameters.operate);

};